package com.zzk.protect.config;

import com.zzk.protect.pojo.User;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class UserSessionManager {
    // 使用线程安全的ConcurrentHashMap存储用户会话
    private final Map<String, User> userSessions = new ConcurrentHashMap<>();

    // 生成随机的sessionId
    private String generateSessionId() {
        return UUID.randomUUID().toString();
    }

    // 添加用户会话
    public String addUserSession(User user) {
        String sessionId = generateSessionId();
        userSessions.put(sessionId, user);
        return sessionId;
    }

    // 获取用户信息
    public User getUserBySessionId(String sessionId) {
        return userSessions.get(sessionId);
    }

    // 移除用户会话
    public void removeUserSession(String sessionId) {
        userSessions.remove(sessionId);
    }
}